package com.example.lib_cache.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_cache.R;
import com.example.lib_cache.dao.OnPoiNameClickListener;
import com.example.lib_cache.databinding.ItemSelectdesBinding;
import com.example.lib_cache.model.Poi;
import com.example.lib_common.util.LiveDataManager;

import java.util.List;

public class PoiAdapter extends RecyclerView.Adapter<PoiAdapter.PoiViewHoder> {

    private List<Poi> itemDatas;

    private Context context;

    public PoiAdapter(List<Poi> itemDatas) {
        this.itemDatas = itemDatas;
    }

    public PoiAdapter(){}

    @NonNull
    @Override
    public PoiAdapter.PoiViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSelectdesBinding itemSelectdesBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_selectdes,
                parent,
                false);
        this.context = parent.getContext();

        return new PoiAdapter.PoiViewHoder(itemSelectdesBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull PoiViewHoder holder, final int position) {
        holder.itemSelectdesBinding.setPoiItem(itemDatas.get(position));
        holder.itemSelectdesBinding.executePendingBindings();
        holder.itemSelectdesBinding.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Log.d("选中了", String.valueOf(position));
                    MutableLiveData<Poi> poiName = LiveDataManager
                            .getInstance().with("PoiInfo", Poi.class);
                    poiName.setValue(itemDatas.get(position));
                if(onPoiNameClickListener!=null) {
                    onPoiNameClickListener.finishThisActivity();
                }

            }
        });
    }

    public void updateData(List<Poi> poiList) {
        if (null == poiList) {
            return;
        }
        itemDatas = poiList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null == itemDatas ? 0 : itemDatas.size();
    }

    class PoiViewHoder extends RecyclerView.ViewHolder{

        ItemSelectdesBinding itemSelectdesBinding;


        public PoiViewHoder(ItemSelectdesBinding itemSelectdesBinding) {
            super(itemSelectdesBinding.getRoot());
            this.itemSelectdesBinding = itemSelectdesBinding;

        }
    }

    private OnPoiNameClickListener onPoiNameClickListener;

    public void setOnPoiNameClickListener(OnPoiNameClickListener listener){
        this.onPoiNameClickListener = listener;
    }
}
